//
//  NNEnvContainerView.m
//  MockQR
//
//  Created by fangshufeng on 2018/3/5.
//  Copyright © 2018年 fangshufeng. All rights reserved.
//

#import "NNEnvContainerView.h"
#import <Foundation/Foundation.h>
#import "KNNEnvCommonMarco.h"

static   CGFloat const kEnvContainerViewMargin = 16;
static   CGFloat const kEnvContainerRowHeight = 32;

@interface NNEnvContainerView ()
/**
 * 行数
 */
@property (nonatomic, assign) int row;

/**
 * 列数
 */
@property (nonatomic, assign) int col;

/**
 * 按钮
 */
@property (nonatomic, strong) NSMutableArray *btns;

/**
 * 上次选中的
 */
@property (nonatomic, strong) UIButton *lastSelectBtn;

/**
 * 环境数组
 */
@property (nonatomic, copy) NSArray *envList;

@end

@implementation NNEnvContainerView

- (instancetype)initWithCol:(int)col envList:(NSArray *)envList{
    if (self =[super init]) {
        self.col = col;
        self.envList = envList;
        
        int index = 0;
        for ( NSString *item in self.envList) {
            UIButton *btn  = [[UIButton alloc] init];
            [btn setTitle:item forState:UIControlStateNormal];
            btn.tag = index;
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            btn.layer.borderColor = KNNENv_rgba(224,224,224,1).CGColor;
            btn.layer.borderWidth = 1;
            btn.layer.cornerRadius = 8;
            btn.clipsToBounds = YES;
            [btn setTitleColor:KNNENv_rgba(173, 173, 196, 1) forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnSelect:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            [self.btns addObject:btn];
            index++;
        }
    }
    return self;
}

- (void)selectIndex:(NSInteger)index {
    NSParameterAssert(index < self.btns.count);
    [self btnSelect:self.btns[index]];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    int k = 0;
    CGFloat width = (self.frame.size.width - kEnvContainerViewMargin * (self.col + 1)) / self.col;
    CGFloat height = kEnvContainerRowHeight;
    
    for (int i = 0 ; i < self.row; i++) {
        for (int j = 0 ; j < self.col; j++) {
            UIButton *btn =  self.btns[k];
            btn.frame = CGRectMake(kEnvContainerViewMargin + (kEnvContainerViewMargin + width) * j,
                                   (kEnvContainerViewMargin + height) * i,
                                   width,
                                   height);
            k++;
            
            if (k == self.btns.count) {
                break;
            }
        }
    }
    
}

- (CGFloat)calculateHeight {
    return (self.row) * kEnvContainerRowHeight + (self.row  - 1) * kEnvContainerViewMargin ;
}

- (int)row {
    NSParameterAssert(self.col > 0);
    return  self.envList.count % self.col == 0 ? ((int)self.envList.count / self.col) : ((int)self.envList.count / self.col + 1);
}

- (void)btnSelect:(UIButton *)btn {
    
    if (btn == self.lastSelectBtn) {
        return;
    }
    
    if (self.itemClick) {
        self.itemClick(btn.tag);
    }
    
    btn.selected = YES;
    [btn setBackgroundColor:KNNENv_rgba(41,107,255,1)];
    self.lastSelectBtn.selected = NO;
    [self.lastSelectBtn setBackgroundColor:[UIColor whiteColor]];
    self.lastSelectBtn = btn;
    
}

- (NSMutableArray *)btns {
    if (_btns == nil) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

@end

